package com.bsj.power.pls.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bsj.power.common.config.cached.RedisCached;
import com.bsj.power.common.config.global.JsonResult;
import com.bsj.power.common.config.global.JsonResultEnum;
import com.bsj.power.common.config.redis.RedisConstant;
import com.bsj.power.common.constant.AlarmEnum;
import com.bsj.power.common.constant.NumericalConstants;
import com.bsj.power.common.def.dto.alarmReport.AlarmLevelDTO;
import com.bsj.power.common.def.entity.alarmLevel.AlarmLevel;
import com.bsj.power.common.def.vo.pls.alarm.AlarmTypeVO;
import com.bsj.power.common.mapper.AlarmLevelMapper;
import com.bsj.power.pls.service.AlarmLevelService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

import static com.bsj.power.common.config.redis.RedisConstant.ALARM_LEVEL_KEY;

/**
 * @author LiJiaXin
 * @version 1.0.0
 * @description AlarmLevelServiceImpl
 * @time 2024/9/6 10:10
 */
@Service
public class AlarmLevelServiceImpl implements AlarmLevelService {

    static final List<Integer> LEVELS = List.of(NumericalConstants.ONE, NumericalConstants.TWO);

    @Resource
    private AlarmLevelMapper alarmLevelMapper;

    @Autowired
    private RedisCached redisCached;

    /**
     * 报警类型列表
     *
     * @return java.util.List<com.bsj.power.common.constant.AlarmEnum>
     * @author ljx
     * @time 2024/9/5 21:53
     */
    @Override
    public List<AlarmTypeVO> listAlarmType() {
        return Arrays.asList(AlarmEnum.values()).stream().map(alarmEnum -> {
            return new AlarmTypeVO(alarmEnum);
        }).collect(Collectors.toList());
    }

    /**
     * 设置报警级别
     *
     * @param alarmLevelDTOS
     * @return com.bsj.power.common.config.global.JsonResult
     * @author ljx
     * @time 2024/9/6 10:55
     */
    @Override
    @Transactional
    public JsonResult setAlarmLevel(List<AlarmLevelDTO> alarmLevelDTOS) {
        List<AlarmLevel> alarmLevels = new LinkedList<>();
        redisCached.del(RedisConstant.ALARM_LEVEL_KEY);
        if(CollectionUtils.isEmpty(alarmLevelDTOS)){
            alarmLevelMapper.delete(null);
        }else{
            for (AlarmLevelDTO alarmLevelDTO : alarmLevelDTOS) {
                Integer level = alarmLevelDTO.getLevel();
                if (level == null) {
                    return JsonResult.fail(JsonResultEnum.ALARM_LEVEL_IS_NULL);
                } else {
                    if (!LEVELS.contains(level)) {
                        return JsonResult.fail(JsonResultEnum.ALARM_LEVEL_NOT_EXIST);
                    }
                }
                List<Integer> codes = alarmLevelDTO.getCodes();
                if (CollectionUtils.isEmpty(codes)) {
                    return JsonResult.fail(JsonResultEnum.ALARM_TYPE_IS_NULL);
                }
                for (Integer code : codes) {
                    String name = AlarmEnum.getNameByCode(code);
                    if (StringUtils.isBlank(name)) {
                        return JsonResult.fail(JsonResultEnum.ALARM_TYPE_NOT_EXIST);
                    }
                    alarmLevels.add(new AlarmLevel(code, name, level));
                }
            }
            //删除原先所有的再新增
            alarmLevelMapper.delete(null);
            alarmLevelMapper.batchInsert(alarmLevels);
            //redis覆盖
            alarmLevelDTOS.forEach(alarmLevelDTO ->
                    redisCached.hset(RedisConstant.ALARM_LEVEL_KEY, alarmLevelDTO.getLevel() + "", JSON.toJSONString(alarmLevelDTO.getCodes())));
        }
        return JsonResult.success();
    }

    /**
     * 报警级别列表
     *
     * @param level
     * @return java.util.List<com.bsj.power.common.def.entity.alarmLevel.AlarmLevel>
     * @author ljx
     * @time 2024/9/6 11:20
     */
    @Override
    public List<AlarmLevel> listAlarmLevel(Integer level) {
        QueryWrapper<AlarmLevel> queryWrapper = new QueryWrapper<>();
        if (level != null) {
            queryWrapper.eq("level", level);
        }
        return alarmLevelMapper.selectList(queryWrapper);
    }

}
